/*
 * FaceCat图形通讯框架
 * 著作权编号:2015SR229355+2020SR0266727
 * 上海卷卷猫信息技术有限公司
 */

using System;
using System.Collections.Generic;

namespace FaceCat {
    /// <summary>
    /// 表格列
    /// </summary>
    public class FCGridColumn : FCButton {
        /// <summary>
        /// 创建列
        /// </summary>
        public FCGridColumn() {
            setWidth(100);
        }

        /// <summary>
        /// 创建列
        /// </summary>
        /// <param name="text">标题</param>
        public FCGridColumn(String text) {
            setText(text);
            setWidth(100);
        }

        /// <summary>
        /// 起始宽度
        /// </summary>
        public int m_beginWidth = 0;

        /// <summary>
        /// 调整大小状态，1:左侧 2:右侧
        /// </summary>
        public int m_resizeState;

        /// <summary>
        /// 触摸按下时的坐标
        /// </summary>
        public FCPoint m_touchDownPoint;

        public bool m_allowDragRow = true;

        /// <summary>
        /// 获取是否可以拖动行
        /// </summary>
        public virtual bool allowDragRow()
        {
            return m_allowDragRow;
        }

        /// <summary>
        /// 设置是否可以拖动行
        /// </summary>
        public virtual void setAllowDragRow(bool value)
        {
            m_allowDragRow = value;
        }

        public bool m_allowResize = false;

        /// <summary>
        /// 获取是否可以调整大小
        /// </summary>
        public virtual bool allowResize() {
            return m_allowResize;
        }

        /// <summary>
        /// 设置是否可以调整大小
        /// </summary>
        public virtual void setAllowResize(bool value) {
            m_allowResize = value;
        }

        public bool m_allowSort = true;

        /// <summary>
        /// 获取是否可以排序
        /// </summary>
        public virtual bool allowSort() {
            return m_allowSort;
        }

        /// <summary>
        /// 设置是否可以排序
        /// </summary>
        public virtual void setAllowSort(bool value) {
            m_allowSort = value;
        }

        public FCHorizontalAlign m_cellAlign = FCHorizontalAlign.Left;

        /// <summary>
        /// 获取内容的横向排列样式
        /// </summary>
        public virtual FCHorizontalAlign getCellAlign() {
            return m_cellAlign;
        }

        /// <summary>
        /// 设置内容的横向排列样式
        /// </summary>
        public virtual void setCellAlign(FCHorizontalAlign value) {
            m_cellAlign = value;
        }

        public String m_columnType = "";

        /// <summary>
        /// 获取列的类型
        /// </summary>
        public virtual String getColumnType() {
            return m_columnType;
        }

        /// <summary>
        /// 设置列的类型
        /// </summary>
        public virtual void setColumnType(String value) {
            m_columnType = value;
        }

        public bool m_frozen = false;

        /// <summary>
        /// 获取是否冻结
        /// </summary>
        public virtual bool isFrozen() {
            return m_frozen;
        }

        /// <summary>
        /// 设置是否冻结
        /// </summary>
        public virtual void setFrozen(bool value) {
            m_frozen = value;
        }

        public FCGrid m_grid;

        /// <summary>
        /// 获取表格
        /// </summary>
        public virtual FCGrid getGrid() {
            return m_grid;
        }

        /// <summary>
        /// 设置表格
        /// </summary>
        public virtual void setGrid(FCGrid value) {
            m_grid = value;
        }

        public FCRect m_headerRect;

        /// <summary>
        /// 获取头部的矩形
        /// </summary>
        public virtual FCRect getHeaderRect() {
            return m_headerRect;
        }

        /// <summary>
        /// 设置头部的矩形
        /// </summary>
        public virtual void setHeaderRect(FCRect value) {
            m_headerRect = value;
        }

        public int m_index = -1;

        /// <summary>
        /// 获取索引
        /// </summary>
        public virtual int getIndex() {
            return m_index;
        }

        /// <summary>
        /// 设置索引
        /// </summary>
        public virtual void setIndex(int value) {
            m_index = value;
        }

        public FCGridColumnSortMode m_sortMode = FCGridColumnSortMode.None;

        /// <summary>
        /// 获取排序状态，0:不排序 1:升序 2:降序
        /// </summary>
        public virtual FCGridColumnSortMode getSortMode() {
            return m_sortMode;
        }

        /// <summary>
        /// 设置排序状态，0:不排序 1:升序 2:降序
        /// </summary>
        public virtual void setSortMode(FCGridColumnSortMode value) {
            m_sortMode = value;
        }

        /// <summary>
        /// 获取视图类型
        /// </summary>
        /// <returns>视图类型</returns>
        public override String getViewType() {
            return "FCGridColumn";
        }

        /// <summary>
        /// 获取属性值
        /// </summary>
        /// <param name="name">属性名称</param>
        /// <param name="value">返回属性值</param>
        /// <param name="type">返回属性类型</param>
        public override void getAttribute(String name, ref String value, ref String type) {
            switch (name)
            {
                case "allowresize":
                    type = "bool";
                    value = FCTran.boolToStr(allowResize());
                    break;
                case "allowdragrow":
                    type = "bool";
                    value = FCTran.boolToStr(allowDragRow());
                    break;
                case "allowsort":
                    type = "bool";
                    value = FCTran.boolToStr(allowSort());
                    break;
                case "cellalign":
                    type = "enum:FCHorizontalAlign";
                    value = FCTran.horizontalAlignToStr(getCellAlign());
                    break;
                case "columntype":
                    type = "text";
                    value = getColumnType();
                    break;
                case "frozen":
                    type = "bool";
                    value = FCTran.boolToStr(isFrozen());
                    break;
                default:
                    base.getAttribute(name, ref value, ref type);
                    break;
            }
        }

        /// <summary>
        /// 获取属性名称列表
        /// </summary>
        /// <returns>属性名称列表</returns>
        public override ArrayList<String> getAttributeNames() {
            ArrayList<String> attributeNames = base.getAttributeNames();
            attributeNames.AddRange(new String[] { "AllowResize", "AllowDragRow", "AllowSort", "CellAlign", "ColumnType", "Frozen" });
            return attributeNames;
        }

        /// <summary>
        /// 点击方法
        /// </summary>
        /// <param name="touchInfo">触摸信息</param>
        public override void onClick(FCTouchInfo touchInfo) {
            base.onClick(touchInfo);
            if (m_resizeState == 0) {
                switch (m_sortMode) {
                    case FCGridColumnSortMode.None:
                    case FCGridColumnSortMode.Desc:
                        m_grid.sortColumn(m_grid, this, FCGridColumnSortMode.Asc);
                        break;
                    case FCGridColumnSortMode.Asc:
                        m_grid.sortColumn(m_grid, this, FCGridColumnSortMode.Desc);
                        break;
                }
            }
        }

        /// <summary>
        /// 拖动开始方法
        /// </summary>
        /// <returns>是否拖动</returns>
        public override bool onDragBegin() {
            return m_resizeState == 0;
        }

        /// <summary>
        /// 拖动中方法
        /// </summary>
        public override void onDragging() {
            base.onDragging();
            if (m_grid != null) {
                ArrayList<FCGridColumn> columns = m_grid.getColumns();
                int columnSize = columns.size();
                for (int i = 0; i < columnSize; i++) {
                    FCGridColumn column = columns.get(i);
                    if (column == this) {
                        FCGridColumn lastColumn = null;
                        FCGridColumn nextColumn = null;
                        int lastIndex = i - 1;
                        int nextIndex = i + 1;
                        while (lastIndex >= 0) {
                            FCGridColumn thatColumn = columns.get(lastIndex);
                            if (thatColumn.isVisible()) {
                                lastColumn = thatColumn;
                                break;
                            }
                            else {
                                lastIndex--;
                            }
                        }
                        while (nextIndex < columnSize) {
                            FCGridColumn thatColumn = columns.get(nextIndex);
                            if (thatColumn.isVisible()) {
                                nextColumn = thatColumn;
                                break;
                            }
                            else {
                                nextIndex++;
                            }
                        }
                        //交换列
                        FCNative native = getNative();
                        int clientX = native.clientX(this);
                        if (lastColumn != null) {
                            int lastClientX = native.clientX(lastColumn);
                            if (clientX < lastClientX + lastColumn.getWidth() / 2) {
                                columns.set(lastIndex, this);
                                columns.set(i, lastColumn);
                                m_grid.update();
                                break;
                            }
                        }
                        if (nextColumn != null) {
                            int nextClientX = native.clientX(nextColumn);
                            if (clientX + column.getWidth() > nextClientX + nextColumn.getWidth() / 2) {
                                columns.set(nextIndex, this);
                                columns.set(i, nextColumn);
                                m_grid.update();
                                break;
                            }
                        }
                        break;
                    }
                }
            }
        }

        /// <summary>
        /// 触摸按下方法
        /// </summary>
        /// <param name="touchInfo">触摸信息</param>
        public override void onTouchDown(FCTouchInfo touchInfo) {
            base.onTouchDown(touchInfo);
            if (touchInfo.m_firstTouch && touchInfo.m_clicks == 1) {
                FCPoint mp = touchInfo.m_firstPoint;
                if (m_allowResize) {
                    if (m_index > 0 && mp.x < 5) {
                        m_resizeState = 1;
                        m_beginWidth = getGrid().getColumn(m_index - 1).getWidth();
                    }
                    else if (mp.x > getWidth() - 5) {
                        m_resizeState = 2;
                        m_beginWidth = getWidth();
                    }
                    m_touchDownPoint = getNative().getTouchPoint();
                }
            }
        }

        /// <summary>
        /// 触摸移动方法
        /// </summary>
        /// <param name="touchInfo">触摸信息</param>
        public override void onTouchMove(FCTouchInfo touchInfo) {
            base.onTouchMove(touchInfo);
            if (m_allowResize) {
                FCPoint mp = touchInfo.m_firstPoint;
                if (m_resizeState > 0) {
                    FCPoint curPoint = getNative().getTouchPoint();
                    int newWidth = m_beginWidth + (curPoint.x - m_touchDownPoint.x);
                    if (newWidth > 0) {
                        if (m_resizeState == 1) {
                            getGrid().getColumn(m_index - 1).setWidth(newWidth);
                        }
                        else if (m_resizeState == 2) {
                            setWidth(newWidth);
                        }
                    }
                    if (m_grid != null) {
                        m_grid.update();
                        m_grid.invalidate();
                    }
                }
                else {
                    if ((m_index > 0 && mp.x < 5) || mp.x > getWidth() - 5) {
                        setCursor(FCCursors.SizeWE);
                    }
                    else {
                        setCursor(FCCursors.Arrow);
                    }
                }
                if (isDragging()) {
                    setCursor(FCCursors.Arrow);
                }
            }
        }

        /// <summary>
        /// 触摸抬起方法
        /// </summary>
        /// <param name="touchInfo">触摸信息</param>
        public override void onTouchUp(FCTouchInfo touchInfo) {
            base.onTouchUp(touchInfo);
            setCursor(FCCursors.Arrow);
            m_resizeState = 0;
            if (m_grid != null) {
                m_grid.invalidate();
            }
        }

        /// <summary>
        /// 重绘前景方法
        /// </summary>
        /// <param name="paint">绘图对象</param>
        /// <param name="clipRect">裁剪区域</param>
        public override void onPaintForeground(FCPaint paint, FCRect clipRect) {
            base.onPaintForeground(paint, clipRect);
            if (getNative() != null && m_grid != null) {
                FCRect rect = new FCRect(0, 0, getWidth(), getHeight());
                int tLeft = rect.right - 15;
                int midTop = rect.top + (rect.bottom - rect.top) / 2;
                long textColor = getPaintingTextColor();
                //升序
                if (m_sortMode == FCGridColumnSortMode.Asc) {
                    FCPoint[] points = new FCPoint[3] 
                    { 
                        new FCPoint(tLeft + 5, midTop - 5), 
                        new FCPoint(tLeft, midTop + 5), 
                        new FCPoint(tLeft + 10, midTop + 5) 
                    };
                    paint.fillPolygon(textColor, points);
                }
                //降序
                else if (m_sortMode == FCGridColumnSortMode.Desc) {
                    FCPoint[] points = new FCPoint[3] 
                    { 
                        new FCPoint(tLeft + 5, midTop + 5), 
                        new FCPoint(tLeft, midTop - 5), 
                        new FCPoint(tLeft + 10, midTop - 5) 
                    };
                    paint.fillPolygon(textColor, points);
                }
            }
        }

        /// <summary>
        /// 设置属性
        /// </summary>
        /// <param name="name">属性名称</param>
        /// <param name="value">属性值</param>
        public override void setAttribute(String name, String value)
        {
            switch (name)
            {
                case "allowresize":
                    setAllowResize(FCTran.strToBool(value));
                    break;
                case "allowdragrow":
                    setAllowDragRow(FCTran.strToBool(value));
                    break;
                case "allowsort":
                    setAllowSort(FCTran.strToBool(value));
                    break;
                case "cellalign":
                    setCellAlign(FCTran.strToHorizontalAlign(value));
                    break;
                case "columntype":
                    setColumnType(value);
                    break;
                case "frozen":
                    setFrozen(FCTran.strToBool(value));
                    break;
                default:
                    base.setAttribute(name, value);
                    break;
            }
        }
    }
}
